package com.ctshk.app.system.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.dto.SysMessageDTO;
import com.ctshk.rpc.system.dto.SysMessageTotalDTO;
import com.ctshk.rpc.system.req.SysMessageListIdReq;
import com.ctshk.rpc.system.req.SysMessageQueryReq;
import com.ctshk.rpc.system.req.SysMessageReq;
import com.ctshk.rpc.system.service.ISystemMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 系统消息表 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2020-12-28
 */
@Api(value = "消息", tags = "消息中心")
@RestController
@RequestMapping("/message")
public class SystemMessageController extends BaseController {

    /**
     * 系统消息服务
     */
    @DubboReference
    private ISystemMessageService systemMessageService;

    @ApiOperation(value = "新增系统消息", notes = "新增系统消息")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增系统消息", operDesc = "新增系统消息")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid SysMessageReq req) {
        return systemMessageService.add(req, getCurrentUser(getRequest()).getId());
    }

    @ApiOperation(value = "批量标记为已读消息", notes = "批量标记为已读消息")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "批量标记为已读消息", operDesc = "批量标记为已读消息")
    @PostMapping("/read-list")
    public Result readList(@RequestBody @Valid SysMessageListIdReq req) {
        return systemMessageService.read(req, getCurrentUser(getRequest()).getId());
    }

    @ApiOperation(value = "查询消息列表", notes = "查询消息列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询消息列表", operDesc = "查询消息列表")
    @PostMapping("/queryList")
    public Result<PageResponse<SysMessageDTO>> queryList(@RequestBody @Valid SysMessageQueryReq sysMessageQueryReq) {
        return Result.success(systemMessageService.queryList(sysMessageQueryReq, getCurrentUser(getRequest()).getId()));
    }


    @ApiOperation(value = "删除消息", notes = "删除消息")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.DELETE, operTitle = "删除消息", operDesc = "删除消息")
    @PostMapping("/delete")
    public Result delete(@RequestBody SysMessageListIdReq req) {
        return systemMessageService.delete(req, getCurrentUser(getRequest()).getId());
    }


    @ApiOperation(value = "标记为已读消息", notes = "标记为已读消息")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "标记为已读消息", operDesc = "标记为已读消息")
    @GetMapping("/readId/{id}")
    public Result readId(@PathVariable Long id) {
        return systemMessageService.read(id, getCurrentUser(getRequest()).getId());
    }

    @ApiOperation(value = "查询未读和待办条数", notes = "查询未读和待办条数")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询未读和待办条数", operDesc = "查询未读和待办条数")
    @GetMapping("/getReadTotal")
    public Result<SysMessageTotalDTO> getReadTotal() {

        return Result.success(systemMessageService.getReadTotal(getCurrentUser(getRequest()).getId()));
    }

    @ApiOperation(value = "全部标为已读", notes = "全部标为已读")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "全部标为已读", operDesc = "全部标为已读")
    @GetMapping("/setAllRead")
    public Result setAllRead() {
        return systemMessageService.setAllRead(getCurrentUser(getRequest()).getId());
    }

}
